class Solution:
    def removeKdigits(self, num: str, k: int) -> str:
        stack = []
        if len(num) - k == 0:
            return '0'
        i = 0
        while i<len(num):
            if len(stack) == 0:
                stack.append(num[i])
            else:
                if stack[-1]> num[i] and i - len(stack) <= k-1:
                    stack.pop(-1)
                    continue
                if len(stack)<len(num)-k:
                    stack.append(num[i])
            i+=1
        k = 0
        for i in range(len(stack)):
            if '0' == stack[i]:
                k = i+1
            else:
                break
        r = "".join(stack[k:])
        if r == '':
            r = '0'
        return r

if __name__ == '__main__':
    so = Solution()
    num = "112"
    k = 1
    r = so.removeKdigits(num, k)
    print(r)
